﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tetris
{

    public class ConsoleRenderer : IRenderer
    {
        private struct RenderMatrixCell
        {
            public char Symbol { get; set; }
            public ConsoleColor Color { get; set; }

            public RenderMatrixCell(char symbol) : this(symbol, ConsoleColor.White)
            {
            }

            public RenderMatrixCell(char symbol, ConsoleColor color) : this()
            {
                this.Symbol = symbol;
                this.Color = color;
            }
        }

        private int renderMatrixRows;
        private int renderMatrixCols;
        private RenderMatrixCell[,] renderMatrix;

        public ConsoleRenderer(int visibleConsoleRows, int visibleConsoleCols)
        {
            renderMatrix = new RenderMatrixCell[visibleConsoleRows, visibleConsoleCols];

            this.renderMatrixRows = renderMatrix.GetLength(0);
            this.renderMatrixCols = renderMatrix.GetLength(1);

            this.ClearBuffer();
        }

        public void EnqueueForRendering(IRenderable renderableFigure)
        {
            char[,] figureImage = renderableFigure.GetImage();

            int imageRows = figureImage.GetLength(0);
            int imageCols = figureImage.GetLength(1);

            MatrixCoords renderTopLeft = renderableFigure.GetTopLeft();

            int lastRow = Math.Min(renderTopLeft.Row + imageRows, this.renderMatrixRows);
            int lastCol = Math.Min(renderTopLeft.Col + imageCols, this.renderMatrixCols);

            for (int row = renderableFigure.GetTopLeft().Row; row < lastRow; row++)
            {
                for (int col = renderableFigure.GetTopLeft().Col; col < lastCol; col++)
                {
                    if (figureImage[row - renderableFigure.GetTopLeft().Row, col - renderableFigure.GetTopLeft().Col] != ' ')
                    {
                        renderMatrix[row, col] = new RenderMatrixCell(
                            figureImage[row - renderableFigure.GetTopLeft().Row, col - renderableFigure.GetTopLeft().Col],
                            renderableFigure.GetColor());
                    }
                }
            }
        }

        public void RenderAll()
        {
            Console.SetCursorPosition(0, 0);

            StringBuilder scene = new StringBuilder();

            for (int row = 0; row < this.renderMatrixRows; row++)
            {
                for (int col = 0; col < this.renderMatrixCols; col++)
                {
                    Console.ForegroundColor = this.renderMatrix[row, col].Color;
                    Console.Write(this.renderMatrix[row, col].Symbol);
                }

                Console.WriteLine();
            }
        }

        public void ClearBuffer()
        {
            for (int row = 0; row < this.renderMatrixRows; row++)
            {
                for (int col = 0; col < this.renderMatrixCols; col++)
                {
                    this.renderMatrix[row, col] = new RenderMatrixCell(' ');
                }
            }
        }

        public void Initialize()
        {
            Console.ResetColor();

            for (int i = 0; i < renderMatrixRows; ++i)
            {
                Console.SetCursorPosition(TetrisMain.WorldCols, i);
                Console.Write("|");
            }

            Console.SetCursorPosition(0, TetrisMain.WorldRows);

            for (int i = 0; i < renderMatrixCols; ++i)
            {
                Console.Write("-");
            }
            Console.Write("!");
        }
    }

}

